<template>
  <div class="app-container">
    <el-page-header @back="backUp" class="backUp" content="添加置顶排行规则"></el-page-header>
    <el-form :ref="formName" :model="formFieldsData" :rules="rules">
      <p class="tips">*收费标准</p>
      <div class="flex_box form_table">
        <div>
          <p class="label">天数</p>
          <p class="label">收费金额（折扣价）</p>
          <p class="label">收费金额（原价）</p>
        </div>
        <div class="flex_box">
          <div class="flex1" v-for="(item,index) in formFieldsData.charge_arr" :key='index'>
            <el-form-item class="el_form_item">
              <el-input v-model="item.day" clearable class="ipt" />
            </el-form-item>
            <el-form-item class="el_form_item">
              <el-input v-model="item.discount_price" clearable class="ipt" />
            </el-form-item>
            <el-form-item class="el_form_item">
              <el-input v-model="item.price" clearable class="ipt" />
            </el-form-item>
          </div>
        </div>
        <p class="el-icon-circle-plus-outline" @click="add"></p>
      </div>
      <p class="tips">*置顶数量限制</p>
      <el-form-item label="前端：" :label-width="formLabelWidth" prop="h5_number">
        <el-input v-model="formFieldsData.h5_number" type='number' style="width: 200px" autocomplete="off" clearable placeholder='请输入最多展示数量'/>
      </el-form-item>
      <!-- <el-form-item label="H5端：" :label-width="formLabelWidth" prop="name">
        <el-input v-model="formFieldsData.name" style="width: 200px" autocomplete="off" clearable placeholder='请输入最多展示数量'/>
      </el-form-item> -->
      <el-form-item class="btns">
        <el-button @click="handlReset" size='medium'>重 置</el-button>
        <el-button type="primary" @click="handleSure" size='medium'>确 定</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import formOperate from '@/layout/mixin/formOperate'
export default {
  name: 'Index',
  mixins: [formOperate],
  data() {
    return {
      formName:'redRule',
      formLabelWidth:'100px',
      formFieldsData:{
        parent:'',
        charge_arr:[
          {days:'',price:'',discount_price:''}
        ],
        h5_number:''
      },
      // 验证规则
      rules: {
        h5_number: [
          { required: true, message: '请输入前端展示数量', trigger: 'blur' }
        ]
      },
    }
  },
  mounted() {
    this.getTopList()
  },
  methods: {
    getTopList() {
      this.$http.get('/promotion/admin/topConfig/top').then(response => {
        console.log(response)
        if (response.code == 10000) {
          if (response.data.top.charge_arr) {
            this.formFieldsData.charge_arr = JSON.parse(response.data.top.charge_arr)
          }
        }
      })
    },
    //  添加标准
    add() {
      this.formFieldsData.charge_arr.push({days:'',price:'',activityPrice:''})
    },
    // 重置
    handlReset() {
      this.company_browser.h5_number = 0
      this.company_browser.charge_arr = []
    },
    // 确定
    handleSure() {
      if (this.formFieldsData.charge_arr.length == 0) {
        this.$message.error('收费标准不能为空')
        return
      }
      let num = 0
      this.formFieldsData.charge_arr.forEach(res=>{
        if (res.day == ''||res.discount_price == ''||res.price == '') {
          num++
        }
      })
      if (num > 0) {
        this.$message.error('收费标准不能为空')
        return
      }
      if (this.formFieldsData.h5_number == '') {
        this.$message.error('请输入前端展示数量')
        return
      }
      let param = {}
      for (let key in this.formFieldsData) {
        param[key] = this.formFieldsData[key]
      }
      param.charge_arr = JSON.stringify(param.charge_arr)
      this.$http.post('/promotion/admin/topConfig', param).then(response => {
        if (response.code == 10000) {
          this.backUp()
        } else {
          this.$message.error(response.message)
        }
      })
    },
    //返回
    backUp() {
      this.$router.go(-1)
    },
  }
}
</script>
<style>
  .details .el-dialog__body{
    height: 700px;
    overflow-y: auto;
  }
</style>
<style lang="scss" scoped>
  .el-form-item--small.el-form-item{
    margin:0 -1px -1px 0!important;
  }
  .title{
    font-size: 16px;
    margin-bottom:10px;
  }
  .info{
    line-height: 30px;
  }
  .img_box{
    text-align: center;
  }
  .job_list{
    border:1px solid #eee;
    padding: 10px;
    border-radius: 4px;
  }
  .backUp{
    margin-bottom: 30px;
  }
  .tips{
    font-size: 14px;
    color:#999;
    margin-bottom: 10px;
  }
  .form_table{
    margin-bottom: 20px;
  }
  .label{
    width: 140px;
    height: 50px;
    line-height: 50px;
    font-size: 14px;
    color:#777;
    padding-left: 4px;
    border:1px solid #eee;
    margin:0 -1px -1px 0;
  }
  .el_form_item{
    display: flex;
    align-items: center;
    justify-content: center;
    height: 50px;
    margin:0;
    padding:0 10px;
    border:1px solid #eee;
    margin:0 -1px -1px 0;
  }
  .ipt{
    width: 80px;
  }
  .el-icon-circle-plus-outline{
    width: 100px;
    height: 147px;
    line-height: 147px;
    text-align: center;
    border:1px solid #eee;
    box-sizing: border-box;
    font-size: 36px;
    color:#777;
    cursor: pointer;
  }
  .el-form-item--small.el-form-item.btns{
    margin-top: 20px!important;
  }
</style>